<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addUser"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list" style="width: 100%">
              <el-table-column
                label="序号"
                type="index"
                :index="indexMethod"
                width="120"
              />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="hAssign(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="adit(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="del(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <div class="block">
                <el-pagination
                  :current-page="pageParams.page"
                  :page-sizes="[1, 2, 3, 5]"
                  :page-size="pageParams.pagesize"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="total"
                  background
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                />
              </div>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <el-dialog
        :title="isEdit?'编辑':'添加'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hClose"
      >
        <el-form ref="roleForm" :model="roleForm" label-width="100px" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="hClose">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>

    <!-- 分配权限弹框 -->
    <el-dialog
      title="分配权限"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialogAssign"
    >
      <assign-role v-if="showDialogAssign" :role-id="curEmployeId" @close="showDialogAssign=false" />
    </el-dialog>
  </div>
</template>

<script>
// 引入组件
import AssignRole from './assignRole'
import { getRoles, delRoles, addRoles, updateRole } from '@/api/setting'
export default {
  name: '',
  components: { AssignRole },
  props: {},
  data() {
    return {
      curEmployeId: '', // 当前的员工编号
      showDialogAssign: false, // 分配权限
      isEdit: false,
      showDialog: false, // 添加/编辑
      list: [],
      total: null,
      pageParams: {
        page: 1,
        pagesize: 2
      },
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
  },
  watch: {},
  created() {
    this.loadRoles()
  },
  mounted() {},
  methods: {
    async loadRoles() {
      try {
        const res = await getRoles(this.pageParams)
        // console.log(res.data)
        this.total = res.data.total
        this.list = res.data.rows
      } catch (err) {
        console.log('获取失败', err)
      }
    },
    handleSizeChange(val) {
      this.loadRoles()
      this.pageParams.pagesize = val
    },
    handleCurrentChange(val) {
      this.loadRoles()
      this.pageParams.page = val
    },
    // 点击新增按钮
    addUser() {
      this.showDialog = true
      this.isEdit = false
    },
    // 删除
    async doDel(id) {
      try {
        await delRoles(id)
        this.$message({ type: 'success', message: '删除成功!' })
        if (this.list.length === 1) {
          this.pageParams.page--
          if (this.pageParams.page <= 0) {
            this.pageParams.page = 1
          }
        }
        this.loadRoles()
      } catch (err) {
        this.$message.error('删除失败')
      }
    },
    // 删除按钮弹出框
    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    indexMethod(index) {
      return (this.pageParams.page - 1) * this.pageParams.pagesize + index + 1
    },
    // 添加功能
    async doAdd() {
      try {
        await addRoles(this.roleForm)
        // 更改页码
        this.pageParams.page = Math.ceil(++this.total / this.pageParams.pagesize)
        // 重新渲染
        this.loadRoles()
        // 弹出提示
        this.$message({ type: 'success', message: '添加成功!' })
        //  关闭弹窗
        this.showDialog = false
      } catch (err) {
        this.$message.error('添加失败')
      }
    },
    // 用户点击了保存
    hSubmit() {
      // 手动兜底校验
      this.$refs.roleForm.validate(valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
          this.hClose()
        }
      })
    },
    // 点击编辑按钮
    adit(row) {
      this.isEdit = true
      this.showDialog = true
      this.roleForm = { ...row }
    },
    // 编辑功能
    async doEdit() {
      try {
        const res = await updateRole(this.roleForm)
        console.log(res)

        this.loadRoles()
        this.$message.success('编辑成功')
        this.showDialog = false
      } catch (err) {
        this.$message.error('编辑失败')
        console.log(err)
      }
    },
    // 表单关闭事件
    hClose() {
      this.showDialog = false
      // 重置表单
      this.roleForm = { // 表单数据项
        name: '',
        description: ''
      }
    },
    hAssign(id) {
      // 记下来id
      this.curEmployeId = id
      this.showDialogAssign = true
    }
  }
}
</script>

<style scoped lang='less'>

</style>
